package com.jiayuan.cn.energy.test.one;

import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.functions.source.RichSourceFunction;
import org.apache.flink.table.data.GenericRowData;
import org.apache.flink.table.data.StringData;

import java.util.Random;

public class WordSourceFunction extends RichSourceFunction {

    private final String[] words = new String[]{
            "Welcome", "to", "the", "world", "of", "flink"
    };
    @Override
    public void run(SourceContext ctx) throws Exception {
        Random random = new Random();
        while (true) {
            int index = random.nextInt(words.length);
            GenericRowData row = new GenericRowData(1);
            row.setField(0,StringData.fromString(words[index]));
            ctx.collect(row);
            Thread.sleep(100);
        }
    }
    @Override
    public void cancel() {
    }
    @Override
    public void open(Configuration parameters) throws Exception {
        super.open(parameters);
    }
}